// 【题目】力扣1996.
// 【难度】中等
// 【提交】https://leetcode.cn/problems/the-number-of-weak-characters-in-the-game/submissions/664837555/
// 【标签】栈、贪心、数组、排序、单调栈、第257场周赛

//
// Created by 1 on 2025/9/21.
//
class Solution {
public:
    int numberOfWeakCharacters(vector<vector<int>>& properties) {
        sort(begin(properties), end(properties), [](const vector<int> & a, const vector<int> & b) {
            return a[0] == b[0] ? (a[1] > b[1]) : (a[0] < b[0]);
        });
        stack<int> st;
        int ans = 0;
        for (auto & p: properties) {
            while (!st.empty() && st.top() < p[1]) {
                ++ans;
                st.pop();
            }
            st.push(p[1]);
        }
        return ans;
    }
};

